package com.ppc.controller.worker;

import com.ppc.result.PageResult;
import com.ppc.result.Result;
import com.ppc.service.WorkerTaskService;
import com.ppc.vo.TaskDetailVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import org.springframework.web.multipart.MultipartFile;

/**
 * 订单相关接口
 *
 * @author Jeferry
 * @date 2023/12/20
 */
@Slf4j
@RestController
@Api(tags = "工人订单接口")
public class WorkerTaskController {

  @Autowired
  private WorkerTaskService taskService;



  /**
   * 分页查询订单
   *
   * @param page       页码
   * @param pageSize   页大小
   * @param taskStatus 订单状态
   * @return {@link Result}<{@link PageResult}>
   */
  @GetMapping("/worker/task/page")
  @ApiOperation("分页查询订单(不含申诉)")
  public Result<PageResult> queryTaskWithStatus(Integer page, Integer pageSize,
      Integer taskStatus) {
    log.info("根据订单状态分页查询订单");
    PageResult p = taskService.queryTaskWithStatus(page, pageSize, taskStatus);
    return Result.success(p);
  }

  /**
   * 分页查询订单（只含申诉）
   * @param page 页码
   * @param pageSize 页大小
   * @return {@link Result}<{@link PageResult}>
   */
  @GetMapping("/worker/task/claim/")
  @ApiOperation("分页查询订单(只含申诉)")
  public Result<PageResult> queryTaskStatusIsClaim(Integer page, Integer pageSize) {
    log.info("根据订单状态分页查询订单，只含申诉");
    PageResult p = taskService.queryTaskStatusIsClaim(page, pageSize);
    return Result.success(p);
  }


  /**
   * 查看订单详情
   *
   * @param taskId 订单id
   * @return {@link Result}<{@link TaskDetailVO}>
   */
  @GetMapping("/worker/task/detail")
  @ApiOperation("查看订单详情")
  public Result<TaskDetailVO> queryTaskInformation(Long taskId) {
    log.info("查看订单具体信息");
    TaskDetailVO t = taskService.queryTaskInformation(taskId);
    return Result.success(t);
  }

  /**
   * 修改订单状态
   *
   * @param taskId     订单id
   * @param taskStatus 订单状态
   * @return {@link Result}
   */
  @GetMapping("/worker/task/status/change")
  @ApiOperation("修改订单状态")
  public Result changeTaskStatus(Long taskId, Integer taskStatus) {
    log.info("修改订单的状态");
    taskService.changeTaskStatus(taskId, taskStatus);
    return Result.success();
  }

  /**
   * 提交订单文件
   *
   * @param taskId     订单id
   * @param fileIdList 文件id列表
   * @return {@link Result}
   */
  @GetMapping("/worker/task/handin")
  @ApiOperation("提交订单文件(不涉及文件上传)")
  public Result handinTask(Long taskId, @RequestParam List<Long> fileIdList) {
    log.info("提交订单文件");
    //调用service层来进行订单文件的上传到数据库
    taskService.handinTask(taskId, fileIdList);
    return Result.success();
  }

  /**
   * 给订单打分
   *
   * @param taskId 需求id
   * @param comment   分数
   * @return {@link Result}
   */
  @GetMapping("/worker/task/comment")
  @ApiOperation("给订单打分")
  public Result commentTask(Long taskId, Integer comment) {
    log.info("评论订单");
    taskService.commentTask(taskId, comment);
    return Result.success();
  }

  /**
   * 判断订单是否已经评分
   *
   * @param taskId 需求id
   * @return {@link Result}<{@link String}>
   */
  @GetMapping("/worker/task/comment/query")
  @ApiOperation("判断订单是否已经评分")
  public Result<String> ifTaskCommented(Long taskId) {
    if (taskService.ifTaskCommented(taskId)) {
      return Result.error("已评论");
    } else {
      return Result.error("未评论");
    }
  }

  /**
   * 查看已完成订单评分
   *
   * @param taskId 需求id
   * @return {@link Result}<{@link Integer}>
   */
  @GetMapping("/worker/task/comment/detail")
  @ApiOperation("查看已完成订单评分")
  public Result<Integer> getEmployerRating(Long taskId) {
    Integer rating = taskService.getEmployerRating(taskId);
    return Result.success(rating);
  }

  @PostMapping("/worker/upload_file")
  @ApiOperation("上传文件")
  public Result<Long> uploadFile( @RequestPart("file") MultipartFile file){
    log.info("文件上传{}",file.getOriginalFilename());
    Long fileId=taskService.uploadFile(file);
    return Result.success(fileId);

  }
}
